//
//  RequestPickUpController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/9/4.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "RequestPickUpController.h"
#import "UITextView+ZWPlaceHolder.h"
#import "SelectProductListController.h"
#import "ShippingAddressController.h"
#import "ShippingAddressModel.h"
@interface RequestPickUpController ()
@property (weak, nonatomic) IBOutlet UITextField *productTextField;
@property (weak, nonatomic) IBOutlet UITextField *addressTextField;
@property (weak, nonatomic) IBOutlet UITextField *countTextField;
@property (weak, nonatomic) IBOutlet UIView *selectedProductBgView;
@property (weak, nonatomic) IBOutlet UILabel *selectedProductLabel;
@property (weak, nonatomic) IBOutlet UILabel *productTotalLabel;
@property (weak, nonatomic) IBOutlet UITextView *remarksTextView;
@property (weak, nonatomic) IBOutlet UILabel *fareLabel;
@property (nonatomic,strong) NSString *freight;
@property (nonatomic,strong) NSString *goodsId;
@property (nonatomic,strong) ShippingAddressModel *addressModel;
@end

@implementation RequestPickUpController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
- (void)setupUI{
    self.title = @"申请提货";
    [self.leftbutton setImage:imgname(@"lefticon") forState:(UIControlStateNormal)];
     self.view.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    
    self.remarksTextView.placeholder = @"请输入备注内容";
    self.freight = @"0";
    self.goodsId = @"";
    [self requestShippingMoneyData];
}
// 获取运费
- (void)requestShippingMoneyData{
    [NetWorkTool POST:getSetting param:nil success:^(id dic) {
        self.freight = [NSString stringWithFormat:@"%@",dic[@"data"][@"freight"]];
        self.fareLabel.text = [NSString stringWithFormat:@"%@元",self.freight];
    } other:^(id dic) {
        
    } fail:^(NSError *error) {
        
    } needUser:YES];
}
- (IBAction)selectProductButtonClick:(UIButton *)sender {
    SelectProductListController * vc = [[SelectProductListController alloc] init];
    vc.selectedProductClick = ^(SelectProductListModel * _Nonnull model) {
        self.productTextField.text = model.name;
        self.selectedProductLabel.text = [NSString stringWithFormat:@"%@可提货数量:",model.name];
        [self requestSelectedProductCountData:model.id];
        self.goodsId = model.id;
        
    };
    [self presentViewController:vc animated:YES completion:nil];
}
// 获取可提货数量
- (void)requestSelectedProductCountData:(NSString *)goodsID {
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userId"] = USER_ID;
    param[@"goodsId"] = goodsID;
    [NetWorkTool POST:getGoodsNum param:param success:^(id dic) {
        self.selectedProductBgView.hidden = NO;
        self.productTotalLabel.text = [NSString stringWithFormat:@"%@",dic[@"data"]];
    } other:^(id dic) {
        
    } fail:^(NSError *error) {
        
    } needUser:YES];
    
}
- (IBAction)selectAddressButtonClick:(UIButton *)sender {
    ShippingAddressController * vc = [[ShippingAddressController alloc] init];
    vc.selectedAddressClick = ^(ShippingAddressModel * _Nonnull model) {
        self.addressTextField.text = [NSString stringWithFormat:@"%@%@",model.areaName,model.address];
        self.addressModel = model;
    };
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)commitButtonClick:(UIButton *)sender {
    
    if ([self.goodsId isEqualToString:@""]) {//
        ShowToastWithText(@"请选择提货商品");
        return;
    }
    if ([self.addressTextField.text isEqualToString:@""]) {//
        ShowToastWithText(@"请选择收货地址");
        return;
    }
    if ([self.countTextField.text isEqualToString:@""]) {//
        ShowToastWithText(@"请输入提货数量");
        return;
    }
    if (self.countTextField.text.integerValue > self.productTotalLabel.text.integerValue) {
        ShowToastWithText(@"可提货物品数量不足");
        return;
    }
    self.remarksTextView.text = [self filterSpaces:self.remarksTextView.text];
    self.remarksTextView.text = [self disable_emoji:self.remarksTextView.text];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userId"] = USER_ID;
    param[@"goodsId"] = self.goodsId;
    param[@"username"] = self.addressModel.name;
    param[@"phone"] = self.addressModel.phone;
    param[@"address"] = [NSString stringWithFormat:@"%@%@",self.addressModel.areaName,self.addressModel.address];
    param[@"remarks"] = self.remarksTextView.text;
    param[@"fare"] = self.freight;
    param[@"num"] = self.countTextField.text;
    [NetWorkTool POST:savePickOrder param:param success:^(id dic) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.navigationController popViewControllerAnimated:YES];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:YES];
}
// 过滤空格
- (NSString *)filterSpaces:(NSString *)text{
    text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
    text = [text stringByReplacingOccurrencesOfString:@"\n"withString:@""];
    return text;
}
/**
 *  过滤字符串中的emoji
 */
- (NSString *)disable_emoji:(NSString *)text{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}
@end
